package com.pe.controller.user;

import javax.persistence.EntityTransaction;

import com.pe.controller.IService;
import com.pe.dao.user.UserFactory;
import com.pe.dao.user.UserFactoryImpl;
import com.pe.model.user.User;
import com.pe.model.user.UserImpl;
import com.pe.service.param.user.RegisterServiceParam;
import com.pe.service.response.IServiceResponse;
import com.pe.service.response.user.RegisterResponse;

public class Register implements IService<RegisterServiceParam>{

	@SuppressWarnings("unchecked")
	@Override
	public IServiceResponse handleService(RegisterServiceParam serviceParam) {
		RegisterResponse response = new RegisterResponse();
		
		User user = new UserImpl(serviceParam.getEmail());
		user.setFirstName(serviceParam.getFirstName());
		user.setLastName(serviceParam.getLastName());
		user.setPassword(serviceParam.getPassword());

		UserFactory uf= new UserFactoryImpl();
		
		 EntityTransaction tr= uf.getEm().getTransaction();
		 try{
			 tr.begin();
			 uf.registerUser(user);
			 tr.commit();
			 response.generateSuccessResponse(serviceParam, null);
		 }catch (Exception e) {
			 response.generateFailureResponse(serviceParam, null);
		}
		return response;
	}

}